import { Component, OnInit, ViewChild } from '@angular/core';
import { SFComponent, SFSchema, SFUISchema } from '@delon/form';
import { NzMessageService } from 'ng-zorro-antd/message';
import { NzModalRef } from 'ng-zorro-antd/modal';
import { SystemService } from 'src/app/routes/sys-setting/services/system.service';

@Component({
  selector: 'app-setting-financial',
  templateUrl: './setting-financial.component.html',
  styleUrls: ['./setting-financial.component.less']
})
export class SettingFinancialComponent implements OnInit {
  @ViewChild('sf', { static: false })
  sf!: SFComponent;
  i: any;
  schema!: SFSchema;
  roleList = [];
  roleNames: any = [];
  constructor(private modal: NzModalRef, public msgSrv: NzMessageService, public service: SystemService) {}

  ngOnInit(): void {
    if (this.i?.id !== 0) {
      this.i.roleIds = this.i.roleId !== '' ? this.i.roleId.split(',') : [];
    }

    this.initSF(this.i);
  }
  initSF(staff: any) {
    this.schema = {
      properties: {
        name: {
          title: '公司名称',
          type: 'string',
          ui: { widget: 'string', placeholder: '请输入公司名称' },
          default: staff.name
        },
        phone: {
          title: '纳税人识别号',
          type: 'string',
          ui: { widget: 'string', placeholder: '请输入纳税人识别号' },
          default: staff.phone
        },
        phone2: {
          title: '税收分类编码',
          type: 'string',
          ui: { widget: 'string', placeholder: '请输入税收分类编码' },
          default: staff.phone
        },
        phone3: {
          title: '发票税率',
          type: 'string',
          ui: { widget: 'string', placeholder: '请输入发票税率' },
          default: staff.phone
        },
        phone4: {
          title: '附加费比例',
          type: 'string',
          ui: { widget: 'string', placeholder: '请输入附加费比例' },
          default: staff.phone
        }
      },
      required: ['name', 'phone', 'phone2', 'phone3', 'phone4']
    };
  }

  sure() {
    if (!this.sf?.value.roleIds || this.sf?.value.roleIds.length === 0) {
      this.service.msgSrv.error('员工角色不能为空！');
      return;
    }
    this.roleNames = [];
    this.roleList.forEach((item: { id: any; roleName: string }) => {
      this.sf?.value.roleIds.forEach((ele: any) => {
        if (ele === item.id) {
          this.roleNames.push(item.roleName);
        }
      });
    });
    if (this.i.id === 0) {
      const params: any = {
        ...this.sf?.value,
        roleId: this.sf?.value.roleIds,
        roleNames: this.roleNames.join(','),
        telephone: this.sf?.value.phone,
        staffName: this.sf?.value.name
      };
      // this.service.request(this.service.$api_addStaff, params).subscribe((res) => {
      //   if (res) {
      //     this.service.msgSrv.success('保存成功！');
      //     this.modal.close(true);
      //   }
      //   // this.showInviteFlag = true;
      //   // this.inviteCode = res.inviteCode;
      // });
    } else {
      const params: any = {
        appUserId: this.i.appUserId,
        staffName: this.sf?.value.name,
        roleId: this.sf?.value.roleIds,
        telephone: this.i.telephone
      };
      // this.service.request(this.service.$api_editorStaff, params).subscribe((res) => {
      //   this.service.msgSrv.success('编辑成功！');
      //   this.modal.close(true);
      // });
    }
  }

  close() {
    this.modal.destroy();
  }
}
